<template>
  <div class="page-container">
    <div class="top-bar">
      <!-- <div class="logo-pic">logo图片</div> -->
    </div>
    <div class="login-container">
      <el-form ref="registeredForm"
               :model="registeredForm"
               :rules="registeredRules"
               class="registered-form"
               auto-complete="on"
               label-position="left">

        <div class="title-container">
          <h3 class="title">账号注册</h3>
        </div>

        <!-- 用户名 -->
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user2" />
          </span>
          <el-input ref="username"
                    v-model="registeredForm.userName"
                    placeholder="请输入用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="on" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password2" />
          </span>
          <el-input :key="passwordType"
                    ref="password"
                    v-model="registeredForm.password"
                    :type="passwordType"
                    placeholder="请输入密码"
                    show-password
                    name="password"/>
        </el-form-item>
        

        <!-- 昵称 -->
        <el-form-item prop="nickName">
          <span class="svg-container">
            <svg-icon icon-class="myname" />
          </span>
          <el-input
                    ref="nickName"
                    v-model="registeredForm.nickName"
                    type="text"
                    placeholder="请输入名称"
                    name="nickName"
                    tabindex="2"
                    auto-complete="on" />
        </el-form-item>
        <!-- 邮箱 -->
        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" class="reIcon"/>
          </span>
          <el-input ref="email"
                    v-model="registeredForm.email"
                    placeholder="请输入邮箱"
                    name="username"
                    type="email"
                    tabindex="1"
                    auto-complete="on" />
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item prop="phoneNumber">
          <span class="svg-container">
            <svg-icon icon-class="phone" class="reIcon"/>
          </span>
          <el-input ref="phoneNumber"
                    v-model="registeredForm.phoneNumber"
                    placeholder="请输入手机号"
                    name="phoneNumber"
                    type="text"
                    tabindex="1"
                    auto-complete="on" />
        </el-form-item>
        <!-- 性别 -->
           <el-form-item label="请选择性别："
                      class="equipments">
              <el-radio v-model="registeredForm.sex" label="男">男</el-radio>
              <el-radio v-model="registeredForm.sex" label="女">女</el-radio>
        </el-form-item>
        
        <!-- 部门 -->
         <el-form-item  label="请选择你所在的部门："  
                        prop="deptId"
                        class="upload2">
            <div class="block">
              <el-cascader
                clearable
                v-model="registeredForm.deptId"
                :options="options"
                :props="{ expandTrigger: 'hover' }"
                @change="handleChange2"></el-cascader>
            </div>
        </el-form-item>

        <!-- 头像 -->
        <el-form-item label="请选择上传的头像：" prop="avatar" class="upload">
         <el-upload
            class="upload-demo"
            action="#"
           :auto-upload="false"
           :on-change="handleChang"
           :show-file-list="false"
            :multiple="false"
            accept="image/png,image/gif,image/ipg,image/jpeg,"
            >
            <el-button size="small" type="primary">点击上传</el-button>

          </el-upload>
        </el-form-item>


        <el-button
                   type="primary"
                   style="width:100%;margin-bottom:30px;margin-left:0px"
                   @click.native.prevent="registered">注册
        </el-button>


        <div class="login">
          已账号？<span class="title" @click="login">立即登录</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { classSelect } from '@/api/classSelect'
import { register } from '@/api/registered'
export default {
  name: 'Registered',
  data() {
    const validatenickName = (rule, value, callback) => { !value == true ? callback(new Error('请输入名称')) : callback()}
    const validateUsername = (rule, value, callback) => { !value == true ? callback(new Error('请输入用户名')) :callback()}
    const validatePassword = (rule, value, callback) => { !value == true ? callback(new Error('请输入密码')) : callback()}
    const validateEmail = (rule, value, callback) => {
      let t = /^[A-Za-zd0-9]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/;
     if(!value == true){
       callback(new Error('请输入邮箱'))
     }else if(!(t.test(value))){
        callback(new Error('邮箱格式错误！'))
     }else{
        callback();
     }
    }
    const validatePhoneNumber = (rule, value, callback) => { 
      const regPhone = /^1[3|4|5|8][0-9]\d{4,8}$/
      console.log(value);
      console.log(!regPhone.test(value));
        if(!value){
          callback(new Error('请输入手机号'))
        }else if( !regPhone.test(value) ){
          callback(new Error('请输入正确的手机号'))
        }else{
          callback()
        }
    }
    return {
      registeredForm: {
        userName : "122460",      /*用户名*/
        password:"123456",        /*密码*/
        deptId:null,            /*部门*/
        nickName : "",      /*昵称*/
        email : "",         /*邮箱*/
        phoneNumber : "",   /*手机号 限制十一位  */
        sex : "",           /*性别   0 男 1 女 2 未知*/
        avatar : "",        /*头像*/
      },
        passwordType: 'password',

      registeredRules: {
        nickName: [ /*名称*/
          { required: true, trigger: 'blur', validator: validatenickName }
        ],
        userName: [ /*用户名*/
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [ /*密码*/
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        email: [ /*邮箱*/
          { required: true, trigger: 'blur', validator: validateEmail}
        ],
        phoneNumber: [ /*手机号*/
          { required: true, trigger: 'blur', validator: validatePhoneNumber}
        ],
        deptId:[
          { required: true,  message: ' 请选择部门'  }
        ]
      },
      


      // 部门
      id: [],
      options:[],

    }
  },
  created(){
    classSelect().then(res =>{
      if(res.code == 200){
       let data = res.data.public;
       console.log(res);
       this.options = data;
      }
    })
  

  },
  methods: {
    // 控制眼睛
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handleChang(file,fileList){
        this.registeredForm.avatar = file.name;
    },
    
     handleChange2(value) {
       if(value){
         console.log(value);
         let data = value[value.length-1];
         this.registeredForm.deptId = data;
       }
    },
    // 跳转到登录
     login(){
      this.$router.push({ path: '/login' })
    },

      // 注册
    registered(){
      register(this.registeredForm).then(res =>{
        if(res.code == 200){
          this.$message({
            message: '注册成功！',
            type: 'success'
          });
        }
    })
    },
  }
}
</script>

<style lang="scss">

$page_bg: #f7f9fe;
$dark_gray: #889aa4;
$light_gray: #dfe4ed;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  .el-form-item__content {
    display: flex;
    height: 40px;
    line-height: 40px;
    border: 1px solid $light_gray;
    border-radius: 5px;

    .svg-container {
      width: 34px;
      color: $dark_gray;
      box-sizing: border-box;
      text-align: center;
      font-size: 20px;
    }

    /* reset element-ui css */
    .el-input {
      flex: 1;
      input {
        height: 40px;
        color: $dark_gray;
        border-radius: 0px 5px 5px 0px;
        -webkit-appearance: none;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $page_bg inset !important;
          -webkit-text-fill-color: $dark_gray !important;
        }
      }
    }
  }
  // 设备单选表单
  .equipments .el-form-item__content {
    display: block;
    border: 0px;
  }
}
</style>

<style lang="scss" scoped>
$page_bg: #f7f9fe;
$dark_gray: #889aa4;
$light_gray: #dfe4ed;
.page-container {
  height: 100%;
  .top-bar {
    height: 60px;
    line-height: 60px;
    background-color: #44b1f4;
  }
  .login-container {
    min-height: calc(100% - 60px);
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: $page_bg;
    .title-container {
      user-select: none;
      color: #333333;
    }
    .registered-form {
      min-width: 600px;
      width: 400px;
      padding: 40px 30px;
      border: 1px solid #cccccc;
      border-radius: 10px;
      background-color: #fff;
      box-shadow: rgba(0, 0, 0, 0.15) 3.2px 3.2px 3.2px;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
}
//头像
.upload  ::v-deep .el-form-item__content{
  border: 0;
}
::v-deep .el-input__inner{
  color: #333333;
}
.upload2 ::v-deep .el-input__inner{
  width: 100%;
  border: 0;
  box-sizing: border-box;
}
.upload2 ::v-deep .block{
   width: 100%;
}
 .upload2 ::v-deep .el-cascader{
 width: 100%;
 }

 .login{
  text-align: center;
    .title{
      color:#005980 ;
      cursor: pointer;
      text-decoration: underline;
    }
}



</style>
